package com.ruoyi.web.controller.construction.supertube;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.supe.Commission;
import com.ruoyi.system.service.customer.CommissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 超管分销佣金Controller
 *
 * @author ruoyi
 * @date 2021-02-05
 */
@CrossOrigin
@RestController
@RequestMapping("/system/commission")
public class CommissionController extends BaseController
{
    @Autowired
    private CommissionService commissionService;

    /**
     * 查询超管分销佣金列表
     */
    @PreAuthorize("@ss.hasPermi('supe:commission:list')")
    @GetMapping("/list")
    public TableDataInfo list(Commission commission)
    {
        startPage();
        List<Commission> list = commissionService.selectCommissionList(commission);
        return getDataTable(list);
    }

    /**
     * 导出超管分销佣金列表
     */
    @PreAuthorize("@ss.hasPermi('supe:commission:export')")
    @Log(title = "超管分销佣金", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Commission commission)
    {
        List<Commission> list = commissionService.selectCommissionList(commission);
        ExcelUtil<Commission> util = new ExcelUtil<Commission>(Commission.class);
        return util.exportExcel(list, "commission");
    }

    /**
     * 获取超管分销佣金详细信息
     */
    @PreAuthorize("@ss.hasPermi('supe:commission:query')")
    @GetMapping(value = "/{commissionId}")
    public AjaxResult getInfo(@PathVariable("commissionId") Long commissionId)
    {
        return AjaxResult.success(commissionService.selectCommissionById(commissionId));
    }

    /**
     * 新增超管分销佣金
     */
    @PreAuthorize("@ss.hasPermi('supe:commission:add')")
    @Log(title = "超管分销佣金", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Commission commission)
    {

        double shuhao = 100;
        BigDecimal ClientMoney = new BigDecimal(Double.toString(commission.getClientMoney()));
        BigDecimal SubstationMoney = new BigDecimal(Double.toString(commission.getSubstationMoney()));
        BigDecimal CommissionMoney = new BigDecimal(Double.toString(commission.getCommissionMoney()));
        BigDecimal cs =new BigDecimal(ClientMoney.add(SubstationMoney).doubleValue());
        double csc = cs.add(CommissionMoney).doubleValue();
        System.err.println("*********************"+csc);

        HashMap<String,Double> map=new HashMap<>();
        map.put("totalRevenue",commission.getTotalRevenue());

        if(map.get("totalRevenue") > shuhao){
            return AjaxResult.error("总收益不能大于 100");
        }


        if(map.get("totalRevenue") <= csc){
            return AjaxResult.error("客户佣金金额、分站佣金金额、超管佣金 总和不能大于总收益");
        }else {
            System.err.println("map.get()******" + map.get("totalRevenue"));
            System.err.println("-*****csc******" + csc);

            commission.setClientMoney(ClientMoney.doubleValue());
            commission.setSubstationMoney(SubstationMoney.doubleValue());
            commission.setCommissionMoney(CommissionMoney.doubleValue());
            commission.setTotalRevenue(map.get("totalRevenue"));

            commission.setModifiedTime(new Date());

            return AjaxResult.success(commissionService.insertCommission(commission));
        }
    }

    /**
     * 修改超管分销佣金
     */
    @PreAuthorize("@ss.hasPermi('supe:commission:edit')")
    @Log(title = "超管分销佣金", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Commission commission)
    {


        double shuhao = 100;
        BigDecimal ClientMoney = new BigDecimal(Double.toString(commission.getClientMoney()));
        BigDecimal SubstationMoney = new BigDecimal(Double.toString(commission.getSubstationMoney()));
        BigDecimal CommissionMoney = new BigDecimal(Double.toString(commission.getCommissionMoney()));
        BigDecimal cs =new BigDecimal(ClientMoney.add(SubstationMoney).doubleValue());
        double csc = cs.add(CommissionMoney).doubleValue();
        System.err.println("*********************"+csc);

        HashMap<String,Double> map=new HashMap<>();
        map.put("totalRevenue",commission.getTotalRevenue());

        if(map.get("totalRevenue") > shuhao){
            return AjaxResult.error("总收益不能大于 100");
        }


        if(10 < csc){
            return AjaxResult.error("客户佣金金额、分站佣金金额、超管佣金 总和不能大于10");
        }else {
            System.err.println("map.get()******" + map.get("totalRevenue"));
            System.err.println("-*****csc******" + csc);

            commission.setClientMoney(ClientMoney.doubleValue());
            commission.setSubstationMoney(SubstationMoney.doubleValue());
            commission.setCommissionMoney(CommissionMoney.doubleValue());
            commission.setTotalRevenue(map.get("totalRevenue"));

            commission.setModifiedTime(new Date());

            return toAjax(commissionService.updateCommission(commission));
        }
    }

    /**
     * 删除超管分销佣金
     */
    @PreAuthorize("@ss.hasPermi('supe:commission:remove')")
    @Log(title = "超管分销佣金", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commissionIds}")
    public AjaxResult remove(@PathVariable Long[] commissionIds)
    {
        return toAjax(commissionService.deleteCommissionByIds(commissionIds));
    }



}
